package values.skill
{
	import core.manager.battle.BattleCountManager;
	import core.manager.battle.BattleEffectManager;
	import core.manager.battle.BattleManager;
	
	import values.avatar.IAvatarValue;

	/**
	 *  选取随机目标进行伤害
	 * @author azhang
	 * 
	 */	
	public class HurtRondomAdditinalEffectValue extends AdditionalEffectValue
	{
		
		public static const HURT_RANDOM_ONE:String = "hurt_random_one";
		
		public function HurtRondomAdditinalEffectValue()
		{
			super();
		}
		
		override public function effect():void
		{
			var tempateV:AdditionalEffectTemplateValue = _skillValue.template.additional;
			//////////////////////////几率没达到///////////////////////////////
			if(!BattleCountManager.countRandom(_skillValue.getLevelValueByTag(tempateV.basePossiblity))) return;
			/////////////////////////被抵抗了/////////////////////////////////
			if(tempateV.canResist && !_skillValue.checkSkillHasLogic(LogicEffectValue.IGNORE_RESIST) && BattleCountManager.countRandom(BattleCountManager.countResistRate(_owner , _hitter))) return;
			
			var effectObj:Object = {};
			var num:Number;
			for each(var proV:AdditionalProTemplateValue in tempateV.proDic)
			{
				if(_hitter[proV.proName] != null)
				{
					if(!proV.isSpecial())
					{
						if(proV.needBaseNum)
						{
							num = _baseDamageNum * _skillValue.getLevelValueByTag(proV.baseNum) / 100;
							_hitter[proV.proName] -= num;
						}
						else
						{
							num = SkillNumTypeKey.getResult(proV.numType , _hitter[proV.proName] , _skillValue.getLevelValueByTag(proV.baseNum));
							_hitter[proV.proName] -= num;
						}
						
						BattleEffectManager.showEffectValue(_owner , _hitter , {num:num , proName:proV.proName});
					}
					else
					{
						var hitterTemp:IAvatarValue = chooseHitter(proV , BattleManager.battleManager.getAvatarsBySide(_hitter.party));
						
						if(proV.needBaseNum)
						{
							num = _baseDamageNum * _skillValue.getLevelValueByTag(proV.baseNum) / 100;
							hitterTemp[proV.proName] -= num;
						}
						else
						{
							num = SkillNumTypeKey.getResult(proV.numType , hitterTemp[proV.proName] , _skillValue.getLevelValueByTag(proV.baseNum));
							hitterTemp[proV.proName] -= num;
						}
						
						hitterTemp[proV.proName] -= num;
						
						BattleEffectManager.showEffectValue(_owner , hitterTemp , {num:num , proName:proV.proName});
					}
				}
			}
		}
		
		protected function chooseHitter(proV:AdditionalProTemplateValue , value:Array):IAvatarValue
		{
			switch(proV.special)
			{
				case HURT_RANDOM_ONE:
					if(value.length <= 1) return null;
					var arr:Array = [];
					for each(var av:IAvatarValue in value)
					{
						if(av != _hitter)
						{
							arr.push(av);
						}
					}
					return arr[Math.floor( Math.random() * arr.length )];
					break;
			}
			
			return null;
		}
	}
}